package assembler;

import java.io.PrintStream;
import java.util.Vector;

class ParsedLine
{

    public ParsedLine(Vector vector)
    {
        operand = new String[4];
        data = new int[8];
        dataCount = 0;
        error = false;
        lineno = ((Integer)vector.elementAt(0)).intValue();
        if(hasLabel(vector))
        {
            label = (String)vector.elementAt(1);
            label = label.substring(0, label.length() - 1).toUpperCase();
            vector.remove(1);
        }
        if(hasDirective(vector))
        {
            try
            {
                directive = (String)vector.elementAt(1);
                vector.remove(1);
            }
            catch(ArrayIndexOutOfBoundsException arrayindexoutofboundsexception)
            {
                System.out.println("Line " + lineno + ": " + "Error parsing assembler directive.");
                error = true;
            }
            for(int i = 1; i < vector.size(); i++)
            {
                try
                {
                    data[i - 1] = (int)getNumValue((String)vector.elementAt(i));
                    dataCount++;
                }
                catch(NumberFormatException numberformatexception)
                {
                    System.out.println("Line " + lineno + ": " + "Error parsing data.");
                    error = true;
                }
                catch(ArrayIndexOutOfBoundsException arrayindexoutofboundsexception1)
                {
                    System.out.println("Line " + lineno + ": " + "Data is limited to 8 items per line.");
                    error = true;
                }
            }

        } else
        {
            Instructions.parse(vector, this);
        }
    }

    public void setInst(String s)
    {
        inst = s;
    }

    public void setAddrMode(int i)
    {
        addrMode = i;
    }

    public void setOperand(int i, String s)
    {
        operand[i] = s;
    }

    public void setExpFactor(int i)
    {
        expFactor = i;
    }

    public void setError()
    {
        error = true;
    }

    public int getLineNum()
    {
        return lineno;
    }

    public String getOperand(int i)
    {
        return operand[i];
    }

    public int getAddrMode()
    {
        return addrMode;
    }

    public String getInst()
    {
        return inst;
    }

    public boolean getError()
    {
        return error;
    }

    public String getLabel()
    {
        return label;
    }

    public int getExpFactor()
    {
        return expFactor;
    }

    public String getDirective()
    {
        return directive;
    }

    public int getDataCount()
    {
        return dataCount;
    }

    public int getData(int i)
    {
        return data[i];
    }

    public String getRawLine()
    {
        return rawLine;
    }

    public String toString()
    {
        String s = "";
        s = "   Line Num: " + lineno + "\n" + "      Label: " + label + "\n" + "Instruction: " + inst + "\n" + "  Operand 1: " + operand[0] + "\n" + "  Operand 2: " + operand[1] + "\n" + "  Operand 3: " + operand[2] + "\n" + "  Directive: " + directive + "\n" + "       Data: ";
        for(int i = 0; i < dataCount; i++)
        {
            s = s + data[i] + ",";
        }

        s = s + "\n";
        return s;
    }

    public static long getNumValue(String s)
        throws NumberFormatException
    {
        byte byte0;
        if(s.charAt(0) == 'b')
        {
            byte0 = 2;
            s = s.substring(1, s.length());
        } else
        if(s.startsWith("0x") || s.startsWith("0X"))
        {
            byte0 = 16;
            s = s.substring(2, s.length());
        } else
        {
            byte0 = 10;
            s = Integer.toString((int)Double.parseDouble(s));
        }
        return Long.parseLong(s, ((int) (byte0)));
    }

    private boolean hasLabel(Vector vector)
    {
        try
        {
            String s = (String)vector.elementAt(1);
            return s.charAt(s.length() - 1) == ':';
        }
        catch(ArrayIndexOutOfBoundsException arrayindexoutofboundsexception)
        {
            return false;
        }
    }

    private boolean hasDirective(Vector vector)
    {
        try
        {
            String s = (String)vector.elementAt(1);
            return s.startsWith(".");
        }
        catch(ArrayIndexOutOfBoundsException arrayindexoutofboundsexception)
        {
            return false;
        }
    }

    public static void main(String args[])
    {
        AsmFileReader asmfilereader = new AsmFileReader("test.asm");
        for(Vector vector = asmfilereader.nextLine(); vector != null; vector = asmfilereader.nextLine())
        {
            ParsedLine parsedline = new ParsedLine(vector);
            System.out.println(((Object) (parsedline)));
        }

    }

    private int lineno;
    private String label;
    private String inst;
    private int addrMode;
    private String directive;
    private String operand[];
    private int dataCount;
    private int data[];
    private int expFactor;
    private boolean error;
    private String rawLine;
}
